<script lang="ts" setup>
import { ref } from 'vue'
// import { showToast } from 'vant'
import { areaList } from '@vant/area-data'

// import { useUserStore } from '@/stores/user'
import { useAddressStore } from '@/stores/address'
import { useRoute } from 'vue-router'
// import router from '@/router'
const route = useRoute()
// const userStore = useUserStore()
const addressStore = useAddressStore()
const onClickLeft = () => history.back()

const searchResult = ref<any>([])

//查找地址对象进行数据回显
const addressInfo = ref<any>({})
const getAddressInfo = () => {
  addressInfo.value = addressStore.list.find((item: { id: any }) => item.id == route.query.id)
}
getAddressInfo()
//新增地址
const onSave = (data: any) => {
  console.log(1111)

  console.log(data)

  data.postalCode = '123321'
  //判断是新增还是修改
  if (route.query.id) {
    addressStore.updateAddress(data)
  } else {
    addressStore.addAddress(data)
  }
}

//删除地址
const onDelete = async () => {
  addressStore.deleteAddress(addressInfo.value.id)
}

//修改默认值
// const changeDefault= (data: any) => {
//   console.log(data);
//   addressStore.isChecked(data.id)

// }
</script>

<template>
  <van-nav-bar :title="addressInfo ? '修改地址' : '新增地址'" left-text="返回" left-arrow @click-left="onClickLeft" />

  <van-address-edit :area-list="areaList" :address-info="addressInfo" :show-delete="addressInfo ? true : false" show-set-default show-search-result :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" @delete="onDelete" />
</template>

<style scoped lang="less"></style>
